/*******************************************************************************
 * File name:     main.c
 * Description:   HTX project- it's a basic framework for LPC2478. It contains
 *                the following directories:
 *                1. _init- low level initialization of CPU, peripherals init
 *                as MAM, PLL, fast I/O and clock for whole CPU modules
 *                2. _drivers- API for LPC2478 peripherals
 *                3. _utils - utilities like printf, itoa, etc
 * Project:       HTX
 * Target:        LPC2478
 * Compiler:      arm-none-eabi-gcc
 * Date:          2010-12-02
 * Author:        Kuba
 * Based on: -
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include <stdbool.h>
# include "LPC24xx.h"
# include "typedef.h"
# include "init.h"
# include "irq.h"
# include "board.h"

# include "timer.h"
# include "uart.h"    /* it's an output for tests with Olimex board */
# include "printf.h"


/*==============================================================================
 Defines
==============================================================================*/
# define RX_DATA_SIZE 8
/*==============================================================================
 Globals
==============================================================================*/
static struct uartConf uart;

static struct timerConf tmr;
static timer t;

static uint8 arrTx[] = "yo uart!";
static uint8 arrRx[RX_DATA_SIZE];
/*==============================================================================
 Static function prototypes
==============================================================================*/
static void jtagDelay      (void);
static void timerService   (void);
static void uartService    (void);
static void uartTimeout    (void);
static void dummyService   (void);
/*==============================================================================
 Main function
==============================================================================*/
int main(void)
{
   /* at the beginning of the main, the following functions are called:
    * - jtagDelay ();   delay necessary for JTAG proper work,
    * - systemInit ();  this function sets MAM (Memory Accelerator Module), PLL,
    *                   fast I/O, maximum speed for peripherals, interrupt vector
    */
   jtagDelay();
   systemInit ();
   vicInit();
   irqEnableCpsr();

   /* PERIPHERALS INITIALIZATION */
   /* leds */
   ledInit();

   /* timer */
   tmr.number              = timer0;
   tmr.priority            = irqLOWEST_PRIORITY;
   tmr.match[1].interrupt  = matchINTERRUPT_ENABLE;
   tmr.match[1].reset      = matchRESET_ENABLE;
   tmr.match[1].delay      = 72000000;
   tmr.match[1].matchService = timerService; /* blink led */
   t = timerInit (&tmr);
   timerMatchEnable(t);

   /* uart */
   uart.baud            = uartBAUD_9600;
   uart.stopBit         = uartSTOP_BIT_1;
   uart.parity          = uartPARITY_DISABLE;
   uart.wordLen         = uartWORD_LENGTH_8BIT;
   uart.txIrq           = uartTX_IRQ_DISABLE;
   uart.rxIrq           = uartRX_IRQ_ENABLE;
   uart.rxIrqLvl        = uartRX_IRQ_TRIGGER_LEVEL_4;
   uart.txIrqService    = dummyService;
   uart.rxIrqService    = uartService;
   uart.timeoutService  = uartTimeout;

   uart2Init (&uart);   /* messages via RS232 */
   uprintf("%s\n", "UART");
   uart2Tx(arrTx, sizeof(arrTx));

   uart2IrqRxEnable(arrRx, sizeof(arrRx));

//   uint32 i;

   while (1)
   {

   }
   return 0;
}

/*==============================================================================
 Static function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:      jtagDelay
 description:     function provides a delay required by jtag
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void jtagDelay (void)
{
   volatile unsigned int i;
   for (i = 0; i < 200000; ++i);
}

/*------------------------------------------------------------------------------
 function name:   timerService
 description:
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void timerService (void)
{
   ledBlink(3);          /* change LED pin's state */
}

/*------------------------------------------------------------------------------
 function name:   uartService
 description:     turn on led
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void uartService(void)
{
   ledOn(2);
   uart2Tx(arrRx, sizeof(arrRx));
}

/*------------------------------------------------------------------------------
 function name:   uartService
 description:     turn on led
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void uartTimeout (void)
{
   uprintf("%s\n", "timeout!");
}

/*------------------------------------------------------------------------------
 function name:   dummyService
 description:     does nothing
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void dummyService (void)
{

}
/******************************************************************************
* END OF FILE
******************************************************************************/
